package com.sj.purchase.modules.cost.repository.imei;

import com.sj.purchase.modules.cost.domain.imei.Inventory;
import com.sj.purchase.modules.cost.domain.imei.InventoryId;
import com.sj.purchase.modules.cost.views.uniquecode.Unique;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * Created by yangrd on 2017/10/16
 **/
public interface InventoryRepository extends JpaRepository<Inventory, InventoryId> {

    Page<Inventory> findByOutStockDateBetweenOrderByOutStockDate(Date start, Date end, Pageable pageable);

    //如果一个imei 存在多个获取最后一个入库的imei
    Inventory findFirstById_ImeiOrderById_InStockDateDesc(String imei);
    List<Inventory> findById_Imei(String imei);

    //查询在库串码
    Page<Inventory> findById_InStockDateBetweenAndOutOrderNoIsNullOrderById_InStockDateDesc(Date start, Date end, Pageable pageable);

    List<Inventory> findByOutOrderNoIn(String[] orderNo);

    List<Inventory> findByOutOrderNo(String orderNo);

    List<Inventory> findByOutStockDateBetweenAndOutOrderNoIsNotNull(Date startOutDate, Date endOutDate);

    @Query("select t.shopName,count(t) from Inventory t where t.outStockDate between  ?1 and ?2 and t.shopName is not null group by t.shopName")
    List<Object[]> countImeiGroupShop(Date start, Date end);
}
